package com.yunhe.sms.controller.active;

import com.yunhe.commens.dto.JsonResponce;
import com.yunhe.commens.dto.Pagedao;
import com.yunhe.commens.util.IPUtils;
import com.yunhe.commens.util.TimeUtils;
import com.yunhe.commens.util.UdpGetClientMacAddr;
import com.yunhe.commens.util.UserAgents;
import com.yunhe.sms.entity.activity.Vote;
import com.yunhe.sms.entity.activity.VoteLog;
import com.yunhe.sms.entity.activity.Voteitem;
import com.yunhe.sms.service.activite.VoteItemService;
import com.yunhe.sms.service.activite.VoteLogService;
import com.yunhe.sms.service.activite.VoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/10/31 0031 15:49
 * @Description: 投票项
 */
@Controller
@RequestMapping("/voteItem")
public class VoteItemController {
    @Autowired
    private VoteItemService voteItemService;
    @Autowired
    private VoteService voteService ;
    @Autowired
    private VoteLogService voteLogService;
    /**
     * 添加页面
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return "active/voteItem/add";
    }

    @RequestMapping("/edit")
    public String edit(Long id, HttpServletRequest req){
        Voteitem voteitem = voteItemService.findId ( id );
        req.setAttribute ( "voteItem",voteitem );
        return "active/voteItem/edit";
    }
    /**
     * 保存
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JsonResponce save(Voteitem voteItem){
        voteItemService.add (voteItem);
        return new JsonResponce ();
    }

    @RequestMapping("/deleted")
    @ResponseBody
    public JsonResponce save(String ids){
        voteItemService.deleted ( ids );
        return new JsonResponce ();
    }

    @RequestMapping("/update")
    @ResponseBody
    public JsonResponce update( Voteitem voteItem){
        voteItemService.update ( voteItem );
        return new JsonResponce ();
    }
    @RequestMapping("/page")
    @ResponseBody
    public  Pagedao<Voteitem> page(int offset,int limit,Long voteId){
        Pagedao<Voteitem> page = voteItemService.page ( offset, limit, voteId );
        return page;
    }


//    投票活动页
    @RequestMapping("/vote")
    public String vote(Long voteId, HttpServletRequest req){
        Vote vote = voteService.findId ( voteId );
        List<Voteitem> voteitems = voteItemService.find ( voteId );
        req.setAttribute ( "voteItems",voteitems );
        req.setAttribute ( "vote",vote );
        if(vote.getStatus ()==0){//如果未发布，则不能访问
            return "";
        }
        return "active/vote/vote";
    }

    @RequestMapping("/count")
    @ResponseBody
    public JsonResponce count(Long id,Long voteId,HttpServletResponse resp,HttpServletRequest req){
        resp.setCharacterEncoding ( "utf-8" );
        String ip = IPUtils.getIp(req);
         UserAgents userAgents = UserAgents.parse(req);//访问类型
        String mac;
        if (!userAgents.isMobile ()){//如果不是手机
            UdpGetClientMacAddr umac = new UdpGetClientMacAddr (ip);
            mac = umac.GetRemoteMacAddr();
        }else{ //手机端无法获取mac地址,用ip区分
            mac = ip;
        }
        String date = TimeUtils.format ( new Date (), TimeUtils.FORMAT_YYYY_MM_DD );
        String mess="投票成功";
        VoteLog voteLog = new VoteLog ( ip, voteId, date, mac );
        int code = voteLogService.find ( voteLog,id );
        if (code==2){
                mess="活动已经结束！";
            }else if (code==2){
                mess="活动还没开始！";
            }else if (code==4) {
                mess="今天已经投过了";
            }
        return new JsonResponce ( code,mess);
    }
}
